<template>
  <div class="upload-wrapper">
    <UploadExcel :on-success="handleSuccess" />
  </div>

</template>

<script>
import { importEmployee } from '@/api/employee'

export default {
  methods: {
    async handleSuccess({ results, header }) {
      console.log('results', results)
      console.log('header', header)
      // 采用了一个映射，中文转换为英文
      const data = results.map(index => {
        const userEn = {}
        userEn.mobile = index['手机号']
        userEn.username = index['姓名']
        userEn.timeOfEntry = new Date(this.formatExcelTime(index['入职日期']))
        userEn.correctionTime = new Date(this.formatExcelTime(index['转正日期']))
        userEn.workNumber = index['工号']
        // 日期另作修改
        return userEn
      })
      console.log('data', data)

      await importEmployee(data)
      this.$message.success('添加成功')
      this.$router.back()
    },
    // 时间日期的转换
    formatExcelTime(num, format = '-') {
      num = Number(num)	// 强制类型转化，以防传来的值是字符串
      let millisecond = 0	// 转化后的毫秒数
      if (num > 60) {
        millisecond = (num - 25569) * 60 * 60 * 24 * 1000
      } else {
        millisecond = (num - 25568) * 60 * 60 * 24 * 1000
      }
      const date = new Date(millisecond)	// 根据转化后的毫秒数获取对应的时间
      const yy = date.getFullYear()
      const mm = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
      const dd = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
      return yy + format + mm + format + dd	// 返回格式化后的日期
    }
  }
}
</script>

<style lang="scss" scoped>
.upload-wrapper{
  margin-top: 200px;
}
</style>
